package com.cofluent.web.client.widget;

import com.cofluent.web.client.model.Build;
import com.cofluent.web.client.provider.DynaTableDataProvider;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HTML;

public class BuildSummary extends Composite
{
	private UnorderedListPanel unorderedListPanel=new UnorderedListPanel();
	
	public static interface BuildSummaryViewer
	{
		public String getSummary(Build build);
	}
	
	private AsyncCallback asyncCallback=new AsyncCallback() {
		public void onFailure(Throwable t) {
			// TODO Auto-generated method stub
		}
		public void onSuccess(Object o) {
			Build[]builds=(Build[]) o;
			while(unorderedListPanel.getWidgetCount() > 0)
			{
				unorderedListPanel.remove(0);
			}
//			for (int i = 0; i < unorderedListPanel.getWidgetCount(); i++)
//			{
//			}
//			unorderedListPanel.remove()
			for (int i = 0; i < builds.length; i++) {
				Build build = builds[i];
				String summary=buildSummaryViewer.getSummary(build);
				unorderedListPanel.add(new HTML(summary));
			}
		}
	};

	private final DynaTableDataProvider provider;

	private final BuildSummaryViewer buildSummaryViewer;

	public BuildSummary(DynaTableDataProvider provider, BuildSummaryViewer buildSummaryViewer)
	{
		this.provider = provider;
		this.buildSummaryViewer = buildSummaryViewer;
		initWidget(unorderedListPanel);
		refresh();
	}

	public void refresh()
	{
		provider.updateRowData(asyncCallback);
	}
}
